//index.js
var util = require('../../utils/util.js')
var timer; // 计时器
var dinnerlist;
var currentlist;

Page({
  data: {
    btntext:"开始"
  },
  
  onLoad: function () {
    console.log("onLoad");
    // setDinner(this);
  },

  onShow: function () {
    console.log("onShow");
    dinnerlist = util.getDinnerList();
    currentlist = dinnerlist.list[dinnerlist.selected];
    setDinner(this);
  },

  // 自定义的开始按钮
  startBtn: function () {
    console.log("开始按钮");
    Countdown(this);
  },

  　　// 自定义的暂停按钮  
  pauseBtn: function () {
    console.log("暂停按钮");
    clearTimeout(timer);
  },

  control: function() {
    if(this.data.btntext == "开始") {
      this.setData({ btntext: "暂停" })
      this.startBtn();
    }
    else {
      this.setData({ btntext: "开始" })
      this.pauseBtn();
    }
  },

  customDinner: function() {
    console.log("customDinner");
    wx.navigateTo({
      url: '../listpage/listpage'
    })
  }
  
})

// 倒计时
function Countdown(page) {
  timer = setTimeout(function () {
    console.log("----Countdown----");
    setDinner(page);
    Countdown(page);
  }, 100);
};

function setDinner(page) {
  var random = Math.round((currentlist.foodlist.length - 1) * Math.random());
  page.setData({ currentdinner: currentlist.foodlist[random] });
};
